package com.samsung.sharepoint;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MotionEvent;

public class Splash extends Activity {

	/**
	 * The thread to process splash screen events
	 */
	private Thread mSplashThread;
    SharedPreferences spref;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Notification("Share Point","Autodetection settings");
		// Splash screen view
		setContentView(R.layout.splash);


		// The thread to wait for splash screen events
		mSplashThread = new Thread() {
			@Override
			public void run() {
				try {
					synchronized (this) {
						// Wait given period of time or exit on touch
						wait(2000);
					}
				} catch (InterruptedException ex) {
					new AlertDialog.Builder(Splash.this)
		    	    .setTitle("Interrupt Exception")
		    	    .setMessage("Interrupt Exception")
		    	    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
		    	        	public void onClick(DialogInterface dialog, int whichButton) {
		    	        		// Stop all Services (except listenService.java) on InterruptException
		    	        		Intent intent1=new Intent("com.samsung.sharepoint.serviceclientclass");
		    	            	stopService(intent1);
		    	            	Intent file_client_intent=new Intent("com.samsung.sharepoint.fileclientservice");
		    	            	stopService(file_client_intent);
		    	            	Intent chat_server_intent=new Intent("com.samsung.sharepoint.serviceclass");
		    	            	stopService(chat_server_intent);
		    	            	Intent file_server_intent=new Intent("com.samsung.sharepoint.fileserverservice");
		    	            	stopService(file_server_intent);
		    	        	}
		    	    })
		    	    .create()
		    	    .show();
				}
				// Shared Preference stores User Name
			    spref = getSharedPreferences("abc",MODE_PRIVATE);
                if(spref.getString("name","Anonymous Pundit").equals("Anonymous Pundit"))
                {	
                	Intent intent = new Intent(Splash.this,first.class);
                	startActivity(intent);
                	finish();
                }else{
            		Intent intent = new Intent("com.samsung.sharepoint.listenservice");
            		startService(intent);
                	Intent intent1 = new Intent("com.samsung.activity.ChatList");
					startActivity(intent1);
					finish();
                }
			}
		};
		mSplashThread.start();

	}

	/**
	 * Processes splash screen touch events
	 */
	@Override
	public boolean onTouchEvent(MotionEvent evt) {
		if (evt.getAction() == MotionEvent.ACTION_DOWN) {
			synchronized (mSplashThread) {
				mSplashThread.notifyAll();
			}
		}

		return true;
	}

	@Override
	public void onDestroy(){
		super.onDestroy();
	}
	
    private void Notification(String notificationTitle, String notificationMessage)
    {
        NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new Notification(R.drawable.abc, "Sharepoint Notification", System.currentTimeMillis());
        //Create a Notification to start/stop "listenService.java" which listens on udp broadcasted message by devices in Wi-Fi
        Intent notificationIntent = new Intent(getBaseContext(), listenServiceNotification.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
 
        notification.setLatestEventInfo(Splash.this, notificationTitle, notificationMessage, pendingIntent);
        notificationManager.notify(1, notification);
    }
}